package purchase.wf;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.XPath;
import org.dom4j.xpath.DefaultXPath;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.Token;

/**
 * jbpm process console
 * 
 * @author Mike
 *
 */
public class JbpmProcessConsoleServlet implements Servlet {

	private static Log log = LogFactory.getLog(JbpmProcessConsoleServlet.class);
	
	public ServletConfig getServletConfig() {
		return this.getServletConfig();
	}

	public String getServletInfo() {
		return this.getServletInfo();
	}

	public void init(ServletConfig servletConfig) throws ServletException {
		log.info("JbpmProcessConsoleServlet init......");
	}
	
	public void destroy() {
		log.info("JbpmProcessConsoleServlet destroy......");
	}

	/**
	 * 编码设置
	 * @param request
	 * @param response
	 */
	private void setReqRespEncoding(ServletRequest request,
			ServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
	}
	
	public void service(ServletRequest req, ServletResponse resp)
			throws ServletException, IOException {
		setReqRespEncoding(req, resp);
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;

		// 如:/jbpm_web_test/aaa.do
		String requestUri = request.getRequestURI();
		// 如：/jbpm_web_test
		String contextPath = request.getContextPath();
		
		JbpmContext jbpmContext = JbpmConfiguration.getInstance().createJbpmContext();
		JbpmProcessConsoleAction jbpmProcessConsoleAction = new JbpmProcessConsoleAction(
				request, response, jbpmContext);
		try {
			
			if (requestUri.equals(contextPath+"/jbpmProcess/processDefinitionList.jbpm")) {
				
			} 

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			jbpmContext.close();
		}
	}

}
